package com.ruoyi.system.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Stop;
import com.ruoyi.system.service.IExitService;
import com.ruoyi.system.service.IStopService;
import com.ruoyi.system.service.ITotalSiteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 停服表Controller
 * 
 * @author zhangkai
 * @date 2021-11-24
 */
@RestController
@RequestMapping("/tietasystem/stop")
public class StopController extends BaseController
{
    @Autowired
    private IStopService stopService;
    
    @Autowired
    private IExitService exitService;
    
    @Autowired
	private ITotalSiteService totalSiteService;

    /**
     * 查询停服表列表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:list')")
    @GetMapping("/list")
    public TableDataInfo list(Stop stop)
    {
        startPage();
        if(stop.getVersion() != null) {
            String newDateRange = stop.getVersion().replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "").substring(0,12);
            stop.setVersion(newDateRange);
            }
        List<Stop> list = stopService.selectStopList(stop);
        return getDataTable(list);
    }

    /**
     * 导出停服表列表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:export')")
    @Log(title = "停服表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Stop stop)
    {
        List<Stop> list = stopService.selectStopList(stop);
        ExcelUtil<Stop> util = new ExcelUtil<Stop>(Stop.class);
        return util.exportExcel(list, "停服表数据");
    }

    /**
     * 获取停服表详细信息
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stopService.selectStopById(id));
    }

    /**
     * 新增停服表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:add')")
    @Log(title = "停服表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stop stop)
    {
        return toAjax(stopService.insertStop(stop));
    }

    /**
     * 修改停服表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:edit')")
    @Log(title = "停服表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stop stop)
    {
        return toAjax(stopService.updateStop(stop));
    }

    /**
     * 删除停服表
     */
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:remove')")
    @Log(title = "停服表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stopService.deleteStopByIds(ids));
    }
    
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Stop> util = new ExcelUtil<Stop>(Stop.class);
        return util.importTemplateExcel("退服数据");
    }
    
    @Log(title = "停服数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('tietasystem:stop:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,String dateRange) throws Exception
    {
        ExcelUtil<Stop> util = new ExcelUtil<Stop>(Stop.class);
        List<Stop> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = stopService.importTotal(dataList, operName,dateRange);
        //清缓存
        cacheClean();
        return AjaxResult.success(message);
    }
    
    private void cacheClean(){
		totalSiteService.cacheClean();
		exitService.cacheClean();
		stopService.cacheClean();
	}
}
